<template>
  <div class="app-container bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
              <div class="flex-1" />
              <el-button type="success" class="bg-green" icon="el-icon-circle-plus" @click="toAdd">新增服务热线</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" width="90" header-align="center" align="center" sortable />
            <el-table-column prop="name" label="服务范围" min-width="260"/>
            <el-table-column prop="user_name" label="工作人员" width="160" header-align="center" align="center"/>
            <el-table-column prop="mobile" label="联系热线" width="200" header-align="center" align="center">
              <template slot-scope="scope">
                {{ scope.row['mobile'] || '--' }}
              </template>
            </el-table-column>
            <el-table-column prop="created_at" label="创建时间" min-width="160"/>
            <el-table-column prop="mobile" label="状态" width="140" header-align="center" align="center">
              <template slot-scope="scope">
                <div @click="setState(scope['row'])">
                  <el-tag v-if="scope['row']['status']===1" type="success">已开启</el-tag>
                  <el-tag v-else type="danger">已禁用</el-tag>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200" header-align="center" align="center" fixed="right">
              <template slot-scope="scope">
                <el-button type="success" class="bg-green" size="small" icon="el-icon-edit" circle @click="toEdit(scope.row)" />
                <el-button type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>

    <el-dialog :visible.sync="dialogVisible" :title="form.title" width="500px" class="dialog" center @close="resetDialog('form')">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="服务范围" prop="name">
          <el-input v-model="form.name" autofocus/>
        </el-form-item>
        <el-form-item label="工作人员" prop="user_name">
          <el-input v-model="form.user_name"/>
        </el-form-item>
        <el-form-item label="联系热线" prop="mobile">
          <el-input v-model="form.mobile"/>
        </el-form-item>
        <el-form-item label="状态" prop="mobile">
          <el-radio-group v-model="form.status" size="small">
            <el-radio-button :label="1">开启</el-radio-button>
            <el-radio-button :label="0">关闭</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitDialog('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange } from '@/utils/table'

export default {
  data() {
    return {
      searchTerms: {
        searchValue: ''
      },
      // 服务范围id
      areaId: '',
      // 服务人员列表
      adminList: [],

      // 弹出框相关数据
      dialogVisible: false,
      form: {
        id: '',
        name: '',
        mobile: '',
        user_name: '',
        // 是否在小程序中显示
        status: 1
      },
      rules: {
        name: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        user_name: [{ required: true, message: '请输入工作人员', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入联系方式', trigger: 'blur' }]
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },

  created() {
    this.areaId = this.$route.query.id;
    this.getData();
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'hotline/list'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
        this.tableLoading = false;
      }).catch(e => {
        this.tableLoading = false;
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取系统用户列表
     *  接口:'user/all-list'
     */
    getUsersList() {
      const url = 'user/admin-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.adminList = res.data['list'][0]['personnel'];
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toAdd() {
      this.form = {}
      this.dialogVisible = true
      this.form.title = '添加服务热线';
      this.form.status = 1;
    },
    toEdit(row) {
      this.form = {}
      this.dialogVisible = true
      this.form = {
        id: row.id,
        name: row.name,
        mobile: row.mobile,
        user_name: row.user_name,
        status: row.status
      }
      this.form.title = '编辑服务热线'
    },

    /**
     * 弹窗表单提交方法,通过判断有没有form_id执行添加或编辑操作
     * formName: form表单中ref的值,即表单名
     */
    submitDialog(formName) {
      this.$refs[formName].validate((valid) => { // 表单验证
        if (valid) {
          const url = this.form.id ? 'hotline/updateHotline' : 'hotline/addHotline';
          const data = this.form;
          this.$store.dispatch('GetConnect', { url, data }).then(res => {
            this.dialogVisible = false;
            this.$message.success(res.msg);
            this.getData();
          }).catch(e => {
            this.$message.error(e.msg + ',请刷新或联系管理员!');
          })
        } else {
          this.$message.error('提交失败,请检查必填项');
        }
      })
    },
    resetDialog(formName) {
      this.$refs[formName].resetFields();
    },

    /**
       * 删除产品
       * row: 当前点击的行的数据
       */
    toDelete(row) {
      this.$confirm('此操作将删除该服务热线, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'hotline/delHotline'
        const data = {
          id: row.id
        }
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.$message.success(res.msg);
          this.getData();
        }).catch(e => {
          this.$message.error(e.msg);
        })
      })
    },

    /**
     * 设置当前服务热线的状态
     * row: 当前点击的行的数据
     */
    setState(row) {
      row.status = row.status === 0 ? 1 : 0;
      const url = 'hotline/updateHotlineStatus'
      const data = { id: row.id, status: row.status }
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.$message.success('修改服务热线状态成功，状态：' + (row.status === 1 ? '已启用' : '已禁用'));
        this.getData();
      }).catch(e => {
        row.status = row.status === 0 ? 1 : 0
        this.$message.error(e.msg + '，请刷新或联系管理员')
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    flex-wrap: wrap;
    > div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      width: 100%;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
.el-tag {
  cursor: pointer;
}
</style>
